package com.pedro.streamer.view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SeekBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.pedro.encoder.input.gl.render.filters.RippleFilterRender;
import com.pedro.encoder.input.gl.render.filters.TornadoFilter;
import com.pedro.encoder.input.gl.render.filters.ZoomFilter;
import com.pedro.streamer.R;
import com.pedro.streamer.manager.VideoArrManager;

public class CameraFilterControlView extends ConstraintLayout implements CompoundButton.OnCheckedChangeListener,SeekBar.OnSeekBarChangeListener {

    private CheckBox tornadoBox, zoomCheckBox, rippCheckBox, shadowCheckBox;
    private SeekBar tornadoMarginSeekBar,zoomMarginSeekBar,waveMarginSeekBar,rippleMarginSeekBar,rippleSpeedSeekBar;
    private VideoArrManager videoArrManager = VideoArrManager.shareInstance();

    public CameraFilterControlView(@NonNull Context context) {
        super(context);

    }

    public CameraFilterControlView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        initView();
    }

    private void initView() {
        tornadoBox = findViewById(R.id.tornado_filter_switch);
        zoomCheckBox = findViewById(R.id.zoom_filter_switch);
        rippCheckBox = findViewById(R.id.wave_filter_switch);
        shadowCheckBox = findViewById(R.id.shadow_filter_switch);

        tornadoBox.setOnCheckedChangeListener(this);
        zoomCheckBox.setOnCheckedChangeListener(this);
        rippCheckBox.setOnCheckedChangeListener(this);
        shadowCheckBox.setOnCheckedChangeListener(this);


        tornadoMarginSeekBar = findViewById(R.id.tornado_margin_seek_bar);
        tornadoMarginSeekBar.setProgress(videoArrManager.getTornadoMargin()*10);
        tornadoMarginSeekBar.setOnSeekBarChangeListener(this);

        zoomMarginSeekBar = findViewById(R.id.zoom_margin_seek_bar);
        zoomMarginSeekBar.setProgress(videoArrManager.getMagnifierMargin());
        zoomMarginSeekBar.setOnSeekBarChangeListener(this);



        rippleMarginSeekBar = findViewById(R.id.ripple_margin_seek_bar);
        rippleMarginSeekBar.setProgress(videoArrManager.getRippleMargin());
        rippleMarginSeekBar.setOnSeekBarChangeListener(this);


        rippleSpeedSeekBar = findViewById(R.id.ripple_speed_seek_bar);
        rippleSpeedSeekBar.setProgress(videoArrManager.getRippleSpeed());
        rippleSpeedSeekBar.setOnSeekBarChangeListener(this);


        waveMarginSeekBar = findViewById(R.id.wave_margin_seek_bar);
        waveMarginSeekBar.setProgress(videoArrManager.getWaveMargin());
        waveMarginSeekBar.setOnSeekBarChangeListener(this);
        reloadView();
    }

    public void reloadView(){

        if (tornadoBox != null){
            tornadoBox.setChecked(videoArrManager.isTornadoFilter);
            tornadoMarginSeekBar.setProgress(videoArrManager.getTornadoMargin());
        }

        if (zoomCheckBox != null){
            zoomCheckBox.setChecked(videoArrManager.isMagnifierFilter);
            zoomMarginSeekBar.setProgress(videoArrManager.getMagnifierMargin()*10);
        }

        if (rippCheckBox != null){
            rippCheckBox.setChecked(videoArrManager.isRippleFilterRender);
            rippleMarginSeekBar.setProgress(videoArrManager.getRippleMargin());
            rippleSpeedSeekBar.setProgress(videoArrManager.getRippleSpeed());
        }


        if (shadowCheckBox != null){
            shadowCheckBox.setChecked(videoArrManager.isWaveFilter);
            waveMarginSeekBar.setProgress(videoArrManager.getWaveMargin());
        }



    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if (!compoundButton.isPressed())
            return;


        int id = compoundButton.getId();
        if (id == R.id.tornado_filter_switch){
            //龙卷风
            if (b){
                videoArrManager.getOpenGlView().addFilter(videoArrManager.getTornadoFilter());
            }else {
                videoArrManager.getOpenGlView().removeFilter(videoArrManager.getTornadoFilter());
            }
            videoArrManager.isTornadoFilter = b;
        } else if (id == R.id.zoom_filter_switch) {
            //放大镜

            if (b){
                videoArrManager.getOpenGlView().addFilter(videoArrManager.getMagnifierFilter());
            }else {
                videoArrManager.getOpenGlView().removeFilter(videoArrManager.getMagnifierFilter());
            }
            videoArrManager.isMagnifierFilter = b;
        } else if (id == R.id.wave_filter_switch) {
            //波浪
            if (b){
                videoArrManager.getOpenGlView().addFilter(videoArrManager.getRippleFilterRender());
            }else {
                videoArrManager.getOpenGlView().removeFilter(videoArrManager.getRippleFilterRender());
            }
            videoArrManager.isRippleFilterRender = b;
        }else if (id == R.id.shadow_filter_switch) {
            //动感虚影
            if (b){
                videoArrManager.getOpenGlView().addFilter(videoArrManager.getWaveFilter());
            }else {
                videoArrManager.getOpenGlView().removeFilter(videoArrManager.getWaveFilter());
            }
            videoArrManager.isWaveFilter = b;
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        if (!b)
            return;

        int seekBarID = seekBar.getId();
        if (seekBarID == R.id.tornado_margin_seek_bar){
            videoArrManager.getTornadoFilter().setTornadoRadius(i/10.0f);
        } else if (seekBarID == R.id.zoom_margin_seek_bar) {
            videoArrManager.getMagnifierFilter().setRadius(i);
        } else if (seekBarID == R.id.wave_margin_seek_bar) {
            videoArrManager.getWaveFilter().setWaveAmplitude(i/10.0f);
        } else if (seekBarID == R.id.ripple_margin_seek_bar) {
            videoArrManager.getRippleFilterRender().setAmplitude(i);
        } else if (seekBarID == R.id.ripple_speed_seek_bar) {
            videoArrManager.getRippleFilterRender().setSpeed(i);
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        int seekBarID = seekBar.getId();
        if (seekBarID == R.id.tornado_margin_seek_bar){
            videoArrManager.setTornadoMargin(seekBar.getProgress());
        }else if (seekBarID == R.id.zoom_margin_seek_bar) {
            videoArrManager.setMagnifierMargin(seekBar.getProgress());
        } else if (seekBarID == R.id.wave_margin_seek_bar) {
            videoArrManager.setWaveMargin(seekBar.getProgress());
        } else if (seekBarID == R.id.ripple_margin_seek_bar) {
            videoArrManager.setRippleMargin(seekBar.getProgress());
        }else if (seekBarID == R.id.ripple_speed_seek_bar) {
            videoArrManager.setBreatheSpeed(seekBar.getProgress());
        }
    }
}
